﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net;
using mesRUpload.UploadDataWebservice;

namespace mesRUpload
{
    class MachineConnRequest : InfoDeal
    {
        private MachineIntegrationServiceService m_MachineIntegrationService;
        private dataCollectForSfcEx m_DataCollectForSfcEx;
        private dataCollectForSfcExResponse m_sfcResponse;

        public MachineConnRequest()
        {
            m_MachineIntegrationService = new MachineIntegrationServiceService();
            m_DataCollectForSfcEx = new dataCollectForSfcEx();
            m_sfcResponse = new dataCollectForSfcExResponse();
        }

        public override long SetConnectInfo(string WSDL, string userName, string password, string timeout)
        {
            Timing.start();
            int time = -1;

            try
            {
                NetworkCredential credential = new NetworkCredential(userName, password);

                m_MachineIntegrationService.Url = WSDL;
                m_MachineIntegrationService.Credentials = credential; // 用户凭证
                m_MachineIntegrationService.PreAuthenticate = true; // 获取或设置是否启用预身份验证

                if (int.TryParse(timeout, out time))
                {
                    m_MachineIntegrationService.Timeout = time;

                    string str = "[webservice]Url=\"" + WSDL
                           + "\" user=\"" + userName
                           + "\" password=\"" + EnDecryptAPI.EnCrypt(password, "mesrupload")
                           + "\" Timeout=\"" + time.ToString() + "\"";
                    string filemessage = "webservice" + "," + "用户信息," + Timing.startTime()
                           + "," + Timing.end() + "," + Timing.spendTime() + "," + "设置正常,"
                           + str;
                    MESUpload.infoLogger.Info(filemessage);
                }
                else
                {
                    //fixme： 日志记录 —— 超时时间异常
                    return CodeCollection.DATA_PARSE_FAIL;
                }

                return CodeCollection.EXECUTE_SUCCESS;
            }
            catch (Exception e)
            {
                string str = "[webservice]Url=\"" + WSDL
                       + "\" user=\"" + userName
                       + "\" password=\"" + EnDecryptAPI.EnCrypt(password, "mesrupload")
                       + "\" Timeout=\"" + time.ToString() + "\"";

                string filemessage = "webservice" + "," + "用户信息," + Timing.startTime()
                       + "," + Timing.end() + "," + Timing.spendTime() + "," + "设置异常,"
                       + Timing.exceptionMess(e) + "," + str;
                //MESUpload.logger.Warn(filemessage);
                MESUpload.errorLogger.Error(filemessage);
                //MESUpload.smtpLogger.Warn(filemessage);

                return CodeCollection.WSDL_EXCEPTION;
            }
        }

        public long SetupResponse(sfcDcExRequest sfcDcExRequest, out int code, out string message)
        {
            int ret_code = -1;
            string ret_message = string.Empty;

            Timing.start();

            try
            {
                m_DataCollectForSfcEx.SfcDcExRequest = sfcDcExRequest;
                m_sfcResponse = m_MachineIntegrationService.dataCollectForSfcEx(m_DataCollectForSfcEx);

                ret_code = m_sfcResponse.@return.code;
                ret_message = m_sfcResponse.@return.message;

                code = ret_code;
                message = ret_message;

                string requestmessage = "[request]设备站点=\"" + sfcDcExRequest.site
                       + "\" 操作用户=\"" + sfcDcExRequest.user
                       + "\" activityId=\"" + sfcDcExRequest.activityId
                       + "\" 过站模式=\"" + sfcDcExRequest.modeProcessSfc
                       + "\" 工位=\"" + sfcDcExRequest.operation
                       + "\" 工位版本=\"" + sfcDcExRequest.operationRevision
                       + "\" 设备资源号=\"" + sfcDcExRequest.resource
                       + "\" DC采集组=\"" + sfcDcExRequest.dcGroup
                       + "\" DC采集组版本号=\"" + sfcDcExRequest.dcGroupRevision
                       + "\" sfc=\"" + sfcDcExRequest.sfc + "\"";
                string filemessage = sfcDcExRequest.sfc + "," + "上传响应" + ","
                    + Timing.startTime() + "," + Timing.end() + "," + Timing.spendTime()
                    + "," + (code == 0 ? "事务正常" : "事务异常") + "," + message
                    + "," + requestmessage + ","
                    + "[response]code=\"" + code + "\" message=\"" + message + "\"";
                MESUpload.logger.Info(filemessage);
                MESUpload.infoLogger.Info(filemessage);

                return CodeCollection.EXECUTE_SUCCESS;
            }
            catch (Exception e)
            {
                code = ret_code;
                message = ret_message;

                string filemessage = sfcDcExRequest.sfc + "," + "上传响应" + ","
                    + Timing.startTime() + "," + Timing.end() + "," + Timing.spendTime() + ","
                    + (code == 0 ? "事务正常" : "事务异常") + "," + Timing.exceptionMess(e);
                MESUpload.logger.Fatal(filemessage);
                MESUpload.errorLogger.Error(filemessage);
                //MESUpload.smtpLogger.Warn(filemessage);

                return CodeCollection.RESPONSE_FAIL;
            }
        }


    }
}
